package com.tian.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.mapper.HonorMapper;
import com.tian.pojo.Honor;
import com.tian.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class HonorServiceImpl implements HonorService{
    @Autowired
    private HonorMapper honorMapper;


    @Override
    public PageResult getList(PageResult pageResult) {
        IPage<Honor> page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Honor> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "honorname", pageResult.getQuery());
        page = honorMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Honor> records = page.getRecords();
        return pageResult.setTotal(total).setRows(records);
    }
    //修改请求
    @Override
    public void updataHonor(Honor honor) {
        honorMapper.updateById(honor);
    }

    //添加请求
    @Override
    @Transactional
    public void addHonor(Honor honor) {
        System.out.println(honor);
        honorMapper.insert(honor);
    }
    //根据ID获取数据
    @Override
    public Honor getById(Integer id) {
        Honor honor = honorMapper.selectById(id);
        return honor;
    }

    @Override
    public void deleteById(Integer id) {
        System.out.println(id);
        honorMapper.deleteById(id);
    }

}
